package com.gsafety.libraryktx.util

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.gsafety.libraryktx.R

import java.io.File

/**
 * 图片下载管理类
 */
class GlideUtils private constructor(private val ctx: Context) {

    val catchDir: File
        get() = GlideApp.getPhotoCacheDir(ctx)!!

    fun displayImage(uri: String, imageView: ImageView, width: Int, height: Int, placeholder: Int, error: Int) {
        GlideApp.with(ctx)
                .load(uri)
                .placeholder(placeholder)
                .error(error)
                .override(width, height)
                .into(imageView)

    }

    fun displayImage(uri: String, imageView: ImageView, placeholder: Int, error: Int) {
        GlideApp.with(ctx)
                .load(uri)
                .placeholder(placeholder)
                .error(error)
                .into(imageView)
    }

     fun displayImage(uri: String, imageView: ImageView) {
            GlideApp.with(ctx)
                    .load(uri)
                    .placeholder(R.mipmap.common_logo_shield)
                    .error(R.mipmap.common_logo_shield)
                    .into(imageView)

    }

    fun displayImage(uri: Uri, imageView: ImageView) {
        GlideApp.with(ctx)
                .load(uri)
                .placeholder(R.mipmap.common_logo_shield)
                .error(R.mipmap.common_logo_shield)
                .into(imageView)
    }

    fun displayImage(resId: Int, imageView: ImageView) {
        GlideApp.with(ctx)
                .load(resId)
                .placeholder(R.mipmap.common_logo_shield)
                .error(R.mipmap.common_logo_shield)
                .into(imageView)
    }

    /**
     * 清除SD卡缓存
     */
    fun cleanDeskCatch() {
        //必须在子线程中执行
        GlideApp.get(ctx).clearDiskCache()
    }

    /**
     * 清除内存缓存
     */
    fun cleanMemoryCatch() {
        //必须在主线程中执行
        GlideApp.get(ctx).clearMemory()
    }

    companion object {
        @Volatile
        private var instance: GlideUtils? = null

        fun getInstance(mContext: Context) =
                instance ?: synchronized(this) {
                    instance ?: GlideUtils(mContext).also { instance = it }
                }

        fun getInstance() = instance
    }
}
